/*
 * Article.cpp
 *
 *  Created on: 2010-7-15
 *      Author: zhzuo
 */

#include "Article.h"

Article::Article(int i){
	this->guid=i;
	this->Init();
}

Article::Article(){
	QString sql="insert into article(title,content,valid) values('','',0)";
	if(Fun::Execute(sql))
	{
		sql="select max(guid) as znum from article";
		QVariant* v=Fun::GetField(sql);
		if(v!=NULL){
			this->guid=v->toInt();
			this->Init();
		}
	}
}

void Article::Init(){
	QString sql="select * from article where guid="+QString::number(this->guid);
	DataRow dr=*Fun::CreateDr(sql);
	this->title=dr["title"].toString();
	this->content=dr["content"].toString();
	this->createtime=dr["createtime"].toInt();
	this->Valid=(bool)dr["valid"].toBool();
	sql="select * from multimedia where articleguid="+QString::number(this->guid)+" order by guid asc";
	this->mmDt=Fun::CreateDt(sql);
}

QString Article::Label(DataRow& dr,bool b){
	QString lab="";
	if(!b){
		lab=MM_BEGIN+dr["guid"].toString()+MM_END;
		return lab;
	}
	int t=dr["type"].toInt();
	switch(t){
		case PICTURE:
			lab=PICTURE_BEGIN+dr["labelid"].toString()+PICTURE_END;
			break;
		case AUDIO:
			lab=AUDIO_BEGIN+dr["labelid"].toString()+AUDIO_END;
			break;
		case VIDEO:
			lab=VIDEO_BEGIN+dr["labelid"].toString()+VIDEO_END;
			break;
		default:
			//throw new Exception(0,"");
			break;
	}
	return lab;
}

QVector<Article::Block>* Article::toSlice(){
	QVector<Article::Block>* vec=new QVector<Article::Block>();
	int c=this->mmDt->RowsCount;
	QString lab1="";
	QString lab2="";
	QString str=this->content;
	for(int i=0;i<c;i++){
		DataRow dr=(*this->mmDt)[i];
		lab1=this->Label(dr,true);
		lab2=this->Label(dr,false);
		str=str.replace(lab1,lab2);
	}

	DataRow* dr;
	QString part1="";
	QString part2="";
	QString mmid="";
	QString pattern=QString(MM_BEGIN)+"[0-9]+"+QString(MM_END);
	QRegExp rx(pattern); 
	int pos = str.indexOf(rx);
	int matchLen=0;
	while(pos>=0){
		matchLen=rx.matchedLength();
		part1=str.mid(0,pos);
		int pos2=pos+matchLen;
		part2=str.mid(pos2,str.length()-pos2);
		mmid=rx.cap(1);
		dr=this->IsMultiMediaID(mmid);
		if(dr!=NULL){
			this->AppendBlock(part1,vec);
			this->AppendBlock(*dr,vec);
			str=part2;
		}
		else
		{
			str=part1+part2;
		}
		pos = str.indexOf(rx);
	}
}

void Article::AppendBlock(QString s,QVector<Article::Block>* vec){
	if(s.trimmed()!=""){
		Block b;
		b.content=s;
		b.guid=-1;
		b.type=0;
		b.path="";
		vec->append(b);
	}
}

void Article::AppendBlock(DataRow& dr,QVector<Article::Block>* vec){
		Block b;
		b.content="";
		b.guid=dr["guid"].toInt();
		b.type=dr["type"].toInt();
		b.path=dr["path"].toString();
		vec->append(b);
}

DataRow* Article::IsMultiMediaID(QString m){
	int d=0;
	try
	{
		d=(int)m;
	}
	catch
	{
		return NULL;
	}
	int c=this->mmDt->RowsCount;
	for(int i=0;i<c;i++){
		DataRow dr=this->mmDt[i];
		if(dr["guid"].toInt()==d){
			return &dr;
		}
	}
	return NULL;
}

bool Article::Update(){
	QString sql="update article set content='"+this->content.replace("'","''")+
			"',title='"+this->title.replace("'","''")+
			"',createtime="+QString::number(this->createtime)+
			",valid="+(this->Valid?"1":"0")+" where guid="+QString::number(this->guid);
	return Fun::Execute(sql);
}

static bool Article::Delete(int g){
	QString sql="delete from multimedia where articleguid="+QString::number(this->guid);
	bool b1=Fun::Execute(sql);
	sql="delete from article where guid="+QString::number(this->guid);
	bool b2=Fun::Execute(sql);
	return b1&b2;
}

bool Article::Delete(int g){
	return Article::Delete(g);
}

int Article::NextLabelID(int t){
	QString sql="";
	switch(t){
		case PICTURE:
			sql="select max(labelid) as znum from multimedia where articleguid="+QString::number(this->guid)+
			" and type="+QString::number(PICTURE);
			break;
		case AUDIO:
			sql="select max(labelid) as znum from multimedia where articleguid="+QString::number(this->guid)+
			" and type="+QString::number(AUDIO);
			break;
		case VIDEO:
			sql="select max(labelid) as znum from multimedia where articleguid="+QString::number(this->guid)+
			" and type="+QString::number(VIDEO);
			break;
		default:
			return 0;
			break;
	}
	QVariant* v=Fun::GetField(sql);
	if(v==NULL){
		return 1;
	}
	int n=v->toInt()+1;
	return n;
}

bool Article::AddMultiMedia(int t,int lid,QString p){
	QString sql="insert into multimedia(articleguid,type,labelid,path) values("+
			QString::number(this->guid)+","+
			QString::number(t)+","+
			QString::number(lid)+",'"+
			p.replace("'","''")+"')";
	return Fun::Execute(sql);
}

bool Article::DelMultiMedia(int g){
	QString sql="delete from multimedia where guid="+QString::number(g);
	return Fun::Execute(sql);
}


